from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework.views import APIView
from rest_framework.response import Response

from .models import ImageModel
from ..utils.response_utils import api_response, ResponseCode

class Top(APIView):
    """
    首页
    """
    # @swagger_auto_schema(
    #     operation_description="企业简介  图片完整url http://10.1.26.11:8080/static/images/top/index_1.jpg",
    #     responses={
    #         200: openapi.Response(
    #             description="访问成功！",
    #             schema=openapi.Schema(
    #                 type=openapi.TYPE_OBJECT,
    #                 properties={
    #                     "code": openapi.Schema(type=openapi.TYPE_INTEGER, description="状态码"),
    #                     "message": openapi.Schema(type=openapi.TYPE_STRING, description="返回信息"),
    #                     "data": openapi.Schema(
    #                         type=openapi.TYPE_ARRAY,
    #                         items=openapi.Schema(
    #                             type=openapi.TYPE_OBJECT,
    #                             properties={
    #                                 "name": openapi.Schema(type=openapi.TYPE_STRING, description="图片名称"),
    #                                 "url": openapi.Schema(type=openapi.TYPE_STRING, description="图片路径"),
    #                                 "description": openapi.Schema(type=openapi.TYPE_STRING, description="文字内容"),
    #                             }
    #                         ),
    #                         description="图片列表"
    #                     )
    #                 }
    #             )
    #         ),
    #         500: openapi.Response(description="服务器内部错误")
    #     },
    #     tags=["首页"]
    # )
    @swagger_auto_schema(
        operation_description="企业简介  图片完整URL示例：http://10.1.26.11:8080/static/images/top/index_1.jpg",
        responses={
            200: openapi.Response(
                description="访问成功！",
                schema=openapi.Schema(
                    type=openapi.TYPE_OBJECT,
                    properties={
                        "code": openapi.Schema(type=openapi.TYPE_INTEGER, description="状态码"),
                        "message": openapi.Schema(type=openapi.TYPE_STRING, description="返回信息"),
                        "data": openapi.Schema(
                            type=openapi.TYPE_OBJECT,
                            properties={
                                "carousel_images": openapi.Schema(
                                    type=openapi.TYPE_ARRAY,
                                    items=openapi.Schema(
                                        type=openapi.TYPE_OBJECT,
                                        properties={
                                            "name": openapi.Schema(type=openapi.TYPE_STRING,
                                                                   description="轮播图片名称"),
                                            "url": openapi.Schema(type=openapi.TYPE_STRING, description="轮播图片路径"),
                                        }
                                    ),
                                    description="轮播图列表"
                                ),
                                "click_jump_images": openapi.Schema(
                                    type=openapi.TYPE_ARRAY,
                                    items=openapi.Schema(
                                        type=openapi.TYPE_OBJECT,
                                        properties={
                                            "name": openapi.Schema(type=openapi.TYPE_STRING,
                                                                   description="跳转图片名称"),
                                            "url": openapi.Schema(type=openapi.TYPE_STRING, description="跳转图片路径"),
                                        }
                                    ),
                                    description="点击跳转图片列表"
                                ),
                                "texts": openapi.Schema(
                                    type=openapi.TYPE_ARRAY,
                                    items=openapi.Schema(
                                        type=openapi.TYPE_OBJECT,
                                        properties={
                                            "title": openapi.Schema(type=openapi.TYPE_STRING, description="数据项标题"),
                                            "value": openapi.Schema(type=openapi.TYPE_STRING, description="数值"),
                                            "description": openapi.Schema(type=openapi.TYPE_STRING,
                                                                          description="单位或后缀"),
                                        }
                                    ),
                                    description="运营数据"
                                )
                            }
                        )
                    }
                )
            ),
            500: openapi.Response(description="服务器内部错误")
        },
        tags=["首页"]
    )
    def get(self, request):
        try:
            # 轮播图片
            carousel = ImageModel.objects.filter(id__in=[1, 2, 3])
            carousel_images = [{"name": img.name, "url": img.image_path} for img in carousel]

            click_jump = ImageModel.objects.filter(id__in=[5, 7, 9, 11])
            click_jump_images = [{"name": img.name, "url": img.image_path} for img in click_jump]

            operational_data = [{"title":"TikTok美区6000+粉账号", "value":"3", "description":"万+"},
                                {"title":"单月GMV", "value":"800", "description":"万"},
                                {"title":"TikTok美区达人库", "value":"1000", "description":"+"}]

            # 添加额外的文字信息
            data = {
                "carousel_images": carousel_images,
                "click_jump_images": click_jump_images,
                "texts":operational_data
            }
            return api_response(ResponseCode.SUCCESS, "访问成功！", data)
        except Exception as e:
            return api_response(ResponseCode.INTERNAL_SERVER_ERROR, str(e))



class ClickToJump(APIView):
    """
    点击跳转内容
    """

    @swagger_auto_schema(
        operation_description="点击跳转内容（有父级图片的ID：5、7、9、11）",
        responses={
            200: openapi.Response(
                description="访问成功！",
                schema=openapi.Schema(
                    type=openapi.TYPE_OBJECT,
                    properties={
                        "code": openapi.Schema(type=openapi.TYPE_INTEGER, description="状态码"),
                        "message": openapi.Schema(type=openapi.TYPE_STRING, description="返回信息"),
                        "data": openapi.Schema(
                            type=openapi.TYPE_OBJECT,
                            properties={
                                "images": openapi.Schema(
                                    type=openapi.TYPE_ARRAY,
                                    items=openapi.Schema(
                                        type=openapi.TYPE_OBJECT,
                                        properties={
                                            "name": openapi.Schema(type=openapi.TYPE_STRING, description="图片名称"),
                                            "url": openapi.Schema(type=openapi.TYPE_STRING, description="图片路径"),
                                        }
                                    ),
                                    description="图片列表"
                                ),
                                "description": openapi.Schema(
                                    type=openapi.TYPE_STRING,
                                    description="文字内容（仅在ID为 5、7、9、11 时返回）"
                                )
                            }
                        )
                    }
                )
            ),
            500: openapi.Response(description="服务器内部错误")
        },
        tags=["首页"]
    )
    def get(self, request, **kwargs):
        try:
            parent = ImageModel.objects.get(id=kwargs['id'])
            children = parent.children.all()
            image_data = [{"name": child.name, "url": child.image_path} for child in children]
            description = {"5": "我们为客户提供从选品、供应链管理、平台运营到品牌推广的全链路服务，尤其专注于TikTok平台的电商运营。通过精准的市场洞察和高效的执行力，帮助客户快速占领市场实现业绩增长。同时，我们利用强大的数据分析能力优化运营策略，确保品牌在全球市场中的高效运营和持续盈利。",
                           "7": "通过我们自有的300平方米专业拍摄中心和先进的内容创作团队，我们为品牌提供优质的原创短视频和图文内容，助力客户在社交平台上实现快速涨粉与品牌曝光。我们的内容创作紧跟市场趋势，结合消费者偏好，创造具有高度互动性和话题性的内容，提升客户在跨境电商平台上的竞争力。",
                           "9": "为了提高内容生产的效率与质量，我们积极布局AIGC技术，利用DEEPSEEK大模型等前沿AI工具优化内容创作流程。通过AI技术，我们不仅提升了内容创作的速度，还保证了内容的精准匹配与个性化定制，进一步增强了品牌传播的效果，为客户在全球市场的数字化转型提供有力支持。",
                           "11": "作为西南跨境电商协会的核心成员，我们致力于为本地区的企业提供出海支持，帮助他们开拓国际市场。通过资源整合、政策支持、培训合作等手段，我们为西南地区的电商企业提供从市场分析、品牌推广到物流解决方案的全方位服务，推动区域内企业快速走向国际舞台，提升竞争力。"}
            data = {
                "images": image_data,
                "description":description.get(kwargs['id'])
            }
            return api_response(ResponseCode.SUCCESS, "访问成功！", data)
        except Exception as e:
            return api_response(ResponseCode.INTERNAL_SERVER_ERROR, str(e))